package app

import (
	"fmt"
	"html/template"
	"log"
	"net/http"
)

func (G *Global) ListProject(w http.ResponseWriter, r *http.Request) {
	//查询所有项目列表
	rows, err := G.DB.SelectAll("select id,name,env from project_info")
	if err != nil {
		log.Println(err)
		fmt.Fprint(w, err)
		return
	}
	//输出渲染模版
	out := map[string]interface{}{"Global": G, "Project": rows}

	t, template_err := template.ParseFiles("templates/configuration/project.html", "templates/common/head.html", "templates/common/foot.html")
	if template_err != nil {
		log.Println(template_err)
		fmt.Fprint(w, template_err)
	} else {
		t.Execute(w, out)
	}
}

func (G *Global) ListConfig(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		//数据库连接
		G.CCC.Db = &G.DB

		//获取表单数据
		id := r.PostFormValue("id")
		name := r.PostFormValue("name")
		env := r.PostFormValue("env")

		//获取配置数据
		err := G.CCC.Init(id, env)
		err = G.CCC.GetConf()
		if err != nil {
			log.Printf("获取项目配置数据出错！\n%s", err)
			w.Write([]byte("获取项目配置数据出错！"))
			return
		}
		G.CCC.Name = name

		//输出渲染模版
		t, template_err := template.ParseFiles("templates/configuration/conflist.html", "templates/common/head.html", "templates/common/foot.html")
		if template_err != nil {
			log.Println(template_err)
			fmt.Fprint(w, template_err)
		} else {
			t.Execute(w, G)
		}
	}
}

func (G *Global) AddConfig(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		//数据库连接
		G.CCC.Db = &G.DB

		//获取表单数据
		id := r.PostFormValue("id")
		name := r.PostFormValue("name")
		env := r.PostFormValue("env")
		key := r.PostFormValue("add-key")
		value := r.PostFormValue("add-value")

		//初始化配置
		err := G.CCC.Init(id, env)
		if err != nil {
			log.Printf("初始化项目配置失败！\n%s", err)
			w.Write([]byte("初始化项目配置失败！"))
			return
		}
		G.CCC.Name = name

		//设置键值
		err = G.CCC.SetKV(key, value)
		if err != nil {
			log.Printf("设置键值失败！\n%s", err)
			w.Write([]byte("设置键值失败！"))
			return
		}

		//获取新的配置
		err = G.CCC.GetConf()
		if err != nil {
			log.Printf("获取项目配置数据出错！\n%s", err)
			w.Write([]byte("获取项目配置数据出错！"))
			return
		}

		//输出渲染模版
		t, template_err := template.ParseFiles("templates/configuration/conflist.html", "templates/common/head.html", "templates/common/foot.html")
		if template_err != nil {
			log.Println(template_err)
			fmt.Fprint(w, template_err)
		} else {
			t.Execute(w, G)
		}
	}
}

func (G *Global) NewVersion(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		//数据库连接
		G.CCC.Db = &G.DB

		//获取表单数据
		id := r.PostFormValue("id")
		name := r.PostFormValue("name")
		env := r.PostFormValue("env")

		//初始化配置
		err := G.CCC.Init(id, env)
		if err != nil {
			log.Printf("初始化项目配置失败！\n%s", err)
			w.Write([]byte("初始化项目配置失败！"))
			return
		}
		G.CCC.Name = name

		err = G.CCC.NewVersion()
		if err != nil {
			log.Printf("复制配置失败！\n%s", err)
			w.Write([]byte("复制配置失败！"))
		}

		//获取新的配置
		err = G.CCC.GetConf()
		if err != nil {
			log.Printf("获取项目配置数据出错！\n%s", err)
			w.Write([]byte("获取项目配置数据出错！"))
			return
		}

		//输出渲染模版
		t, template_err := template.ParseFiles("templates/configuration/conflist.html", "templates/common/head.html", "templates/common/foot.html")
		if template_err != nil {
			log.Println(template_err)
			fmt.Fprint(w, template_err)
		} else {
			t.Execute(w, G)
		}
	}
}

func (G *Global) ChangeVersion(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		//数据库连接
		G.CCC.Db = &G.DB

		//获取表单数据
		id := r.PostFormValue("id")
		name := r.PostFormValue("name")
		env := r.PostFormValue("env")
		chver := r.PostFormValue("chver")

		//初始化配置
		err := G.CCC.Init(id, env)
		if err != nil {
			log.Printf("初始化项目配置失败！\n%s", err)
			w.Write([]byte("初始化项目配置失败！"))
			return
		}
		G.CCC.Name = name

		//设置版本号
		G.CCC.SetActiveVersion(chver)

		//获取配置
		err = G.CCC.GetConf()
		if err != nil {
			log.Printf("获取项目配置数据出错！\n%s", err)
			w.Write([]byte("获取项目配置数据出错！"))
			return
		}

		//输出渲染模版
		t, template_err := template.ParseFiles("templates/configuration/conflist.html", "templates/common/head.html", "templates/common/foot.html")
		if template_err != nil {
			log.Println(template_err)
			fmt.Fprint(w, template_err)
		} else {
			t.Execute(w, G)
		}
	}
}

func (G *Global) DelConfig(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		//数据库连接
		G.CCC.Db = &G.DB

		//获取表单数据
		id := r.PostFormValue("id")
		env := r.PostFormValue("env")
		key := r.PostFormValue("key")

		//初始化配置
		err := G.CCC.Init(id, env)
		if err != nil {
			log.Printf("初始化项目配置失败！\n%s", err)
			fmt.Fprintf(w, "error")
			return
		}

		//删除配置
		err = G.CCC.DelKV(key)
		if err != nil {
			log.Printf("删除配置失败！\n%s", err)
			fmt.Fprintf(w, "error")
		} else {
			fmt.Fprintf(w, "succeed")
		}
	}
}

func (G *Global) EditConfig(w http.ResponseWriter, r *http.Request) {
	if r.Method == "POST" {
		//数据库连接
		G.CCC.Db = &G.DB

		//获取表单数据
		id := r.PostFormValue("id")
		env := r.PostFormValue("env")
		name := r.PostFormValue("name")
		key := r.PostFormValue("edit-key")
		value := r.PostFormValue("edit-value")

		//初始化配置
		err := G.CCC.Init(id, env)
		if err != nil {
			log.Printf("初始化项目配置失败！\n%s", err)
			fmt.Fprintf(w, "error")
			return
		}
		G.CCC.Name = name

		err = G.CCC.SetKV(key, value)
		if err != nil {
			log.Printf("修改键值失败！\n%s", err)
			w.Write([]byte("修改键值失败！"))
			return
		}

		//获取新的配置
		err = G.CCC.GetConf()
		if err != nil {
			log.Printf("获取项目配置数据出错！\n%s", err)
			w.Write([]byte("获取项目配置数据出错！"))
			return
		}

		//输出渲染模版
		t, template_err := template.ParseFiles("templates/configuration/conflist.html", "templates/common/head.html", "templates/common/foot.html")
		if template_err != nil {
			log.Println(template_err)
			fmt.Fprint(w, template_err)
		} else {
			t.Execute(w, G)
		}
	}
}

//列出项目列表，包含token列
func (G *Global) ListToken(w http.ResponseWriter, r *http.Request) {
	//查询所有项目列表
	rows, err := G.DB.SelectAll("select id,name,env,token from project_info")
	if err != nil {
		log.Println(err)
		fmt.Fprint(w, err)
		return
	}
	//输出渲染模版
	out := map[string]interface{}{"Global": G, "Project": rows}

	t, template_err := template.ParseFiles("templates/configuration/token.html", "templates/common/head.html", "templates/common/foot.html")
	if template_err != nil {
		log.Println(template_err)
		fmt.Fprint(w, template_err)
	} else {
		t.Execute(w, out)
	}
}

//生成新的token
func (G *Global) NewToken(w http.ResponseWriter, r *http.Request) {
	id := r.PostFormValue("id")
	env := r.PostFormValue("env")
	token := G.RandStr(64)

	_, err := G.DB.Update("update project_info set token=? where id=? and env=?", token, id, env)
	if err != nil {
		log.Println(err)
		fmt.Fprint(w, "生成token出错")
		return
	}
	G.ListToken(w, r)
}
